package org.snusmumr.android.media.tools;

import org.snusmumr.android.media.util.Utils;

public class ModelAuthor {
	private String[] authorNameArray;
	private String authorName;
	private Long id;

	public ModelAuthor(String authorName) {
		if(! Utils.isEmpty(authorName)) {
			this.authorName = authorName.trim();
			this.authorNameArray = authorName.split(" ");
		} else
			throw new IllegalStateException("Author name is null or empty");
	}
	
	public ModelAuthor(String authorName, Long id) {
		this(authorName);
		this.id = id;
	}
	
	public boolean isEqual(String otherAuthor) {
		if(Utils.isEmpty(otherAuthor))
			return false;
		
		if(! authorName.equalsIgnoreCase(otherAuthor.trim())) {
			String[] nameArray = otherAuthor.split(" ");
			if(authorNameArray.length != nameArray.length)
				return false;
			for (int i = 0; i < authorNameArray.length; i++) {
				boolean found = false;
				String namePart = authorNameArray[i].trim();
				for (int j = 0; j < nameArray.length; j++) {
					if(namePart.equalsIgnoreCase(nameArray[j].trim())) {
						found = true;
						break;
					}
				}
				if(! found)
					return false;
			}
		}
		return true;
	}
	
	public boolean isEqual(ModelAuthor model) {
		if(model == null)
			return false;
		if(model.id != null && this.id != null)
			return this.id.equals(model.id);
		return isEqual(model.authorName);
	}

}
